package com.xiaogao.centerEdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaogao.commonUtils.JwtUtils;
import com.xiaogao.commonUtils.utils.MD5;
import com.xiaogao.serviceBase.utils.exceptionHandler.XiaoGaoException;
import com.xiaogao.centerEdu.entity.UcenterMember;
import com.xiaogao.centerEdu.entity.vo.LoginVo;
import com.xiaogao.centerEdu.entity.vo.RegisterVo;
import com.xiaogao.centerEdu.mapper.UcenterMemberMapper;
import com.xiaogao.centerEdu.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 小高
 * @since 2021-06-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 会员登录
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        // 校验参数
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new XiaoGaoException(20001,"error");
        }
        // 获取会员
        UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (member == null){
            throw new XiaoGaoException(20001,"error");
        }
        // 校验密码
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new XiaoGaoException(200001,"error");
        }
        // 校验是否被禁用
        if (member.getIsDisabled()){
            throw new XiaoGaoException(20001,"error");
        }
        // 使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    /**
     * 会员注册
     * @param registerVo
     */
    @Override
    public boolean register(RegisterVo registerVo) {
        // 获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        // 校验参数
        if (StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new XiaoGaoException(20001,"error");
        }

        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        // 校验验证码
        if(!code.equals(mobleCode)) {
            throw new XiaoGaoException(20001,"error");
        }

        // 校验数据库是否存在相同的手机号
        Integer mobileCount = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (mobileCount.intValue() > 0 ){
            throw new XiaoGaoException(20001,"error");
        }
        // 添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        int insert = baseMapper.insert(member);
        if (insert <=0){
            return false;
        }
        return true;
    }

    /**
     * 根据微信id查询用户信息
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    /**
     * 统计某一天注册人数
     * @param day 日期
     * @return
     */
    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.selectRegisterCount(day);
    }


}
